package logic

import (
	"context"
	"develop/model/mysql"
	"develop/usersrpc/internal/svc"
	"develop/usersrpc/usersrpc"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"strings"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *usersrpc.CommentAddRequest) (*usersrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUserId mysql.User_info
	err := findUserId.FindId(mysql.DB, in.UserId)
	if err != nil {
		return nil, err
	}
	if findUserId.ID == 0 {
		return nil, errors.New("请先登录")
	}
	if in.Comment_Content == "" {
		return nil, errors.New("请输入评论内容")
	}
	if strings.Contains(in.Comment_Content, "垃圾") || strings.Contains(in.Comment_Content, "骗人") {
		return nil, errors.New("含违规词汇，请修改")
	}
	comment := &mysql.Comment_Info{
		User_id:         int(in.UserId),
		Comment_Content: in.Comment_Content,
		Is_Anonymous:    int(in.Is_Anonymous),
	}
	err = comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &usersrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
